package com.qianfeng.yime;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.qianfeng.yime.adapter.CommonFragmentAdapter;
import com.qianfeng.yime.fragment.FindFragment;
import com.qianfeng.yime.fragment.HomeFragment;
import com.qianfeng.yime.fragment.JianZhiFragment;
import com.qianfeng.yime.model.User;
import com.qianfeng.yime.util.CommonUtils;
import com.qianfeng.yime.util.CurrentSingleton;

import java.util.LinkedList;

/**
 *
 */
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener,
        RadioGroup.OnCheckedChangeListener {

    private Context context;
    private RadioGroup radioGroup;
    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        context = this;
        initSlidingMenu();

        radioGroup = (RadioGroup) findViewById(R.id.main_bottom_bar_rg);

        viewPager = (ViewPager) findViewById(R.id.pager_main);
        LinkedList<Fragment> fragments = new LinkedList<Fragment>();
        fragments.add(new HomeFragment());
        fragments.add(new JianZhiFragment());
        fragments.add(new FindFragment());

        CommonFragmentAdapter adapter = new CommonFragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);

        viewPager.setOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);
        // 设置初始为第一个界面 “首页”，ViewPager 默认显示第0项
        RadioButton rbHome = (RadioButton) radioGroup.getChildAt(0);
        rbHome.setChecked(true);

        /**
         * 测试用户是否登录成功
         */
        CurrentSingleton singleton = CurrentSingleton.getInstance();
        User user = singleton.getUser();
        if (user != null) {
            int id = user.getId();
            System.out.println("id = " + id);
        }
    }

    /**
     * 添加 SlidingMenu,并且响应 SlidingMenu 中的操作
     */
    public void initSlidingMenu() {
        // TODO 添加侧滑菜单
        SlidingMenu slidingMenu = new SlidingMenu(this, SlidingMenu.LEFT);
        slidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_MARGIN);
        // 设置偏移量为100dp
        slidingMenu.setBehindOffset(CommonUtils.dip2px(this, 60));
        slidingMenu.setFadeEnabled(false);
        View slidingView = getLayoutInflater().inflate(R.layout.sliding_mine, null);
        // TODO 响应 SlidingMenu 中的操作
        Button btnLogin = (Button) slidingView.findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new OnClickSlidingListener());


        slidingMenu.setMenu(slidingView);


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(position);
        radioButton.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO
        switch (checkedId) {
            case R.id.main_bottom_bar_home:
                viewPager.setCurrentItem(0);
                break;
            case R.id.main_bottom_bar_jianzhi:
                viewPager.setCurrentItem(1);
                break;
            case R.id.main_bottom_bar_find:
                viewPager.setCurrentItem(2);
                break;

        }
        // 如果不设置ID则需要这样处理
//        int count = group.getChildCount();
//        for (int i = 0; i < count; i++) {
//            RadioButton rb = (RadioButton) group.getChildAt(i);
//            if (rb.isChecked()) {
//                viewPager.setCurrentItem(i);
//                break;
//            }
//        }
    }

    /**
     * 处理 SlidingMenu 上的点击事件的监听器
     */
    class OnClickSlidingListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_user_icon:  // 头像
                    break;

                case R.id.btn_login: // 登录
                    // TODO 登录
                    Intent intent = new Intent(context, UserLoginActivity.class);
                    startActivity(intent);
//                    startActivityForResult(intent,);
                    // 登录成功后应该修改登录用户名...
                    break;
                case R.id.mine_card_exit: // 退出
                    // TODO 退出应用，如果当期有用户登录，应该注销
                    break;

            }
        }
    }
}
